<?php

namespace loggedin\controller;

require_once("/loggedin/view/memberAreaView.php");
require_once("/login/view/HTMLView.php");
require_once("/common/view/View.php");
require_once("/loggedin/view/QuestionView.php");
require_once("/loggedin/model/GetQuestions.php");
require_once("/login/view/Userinfo.php");
require_once("/loggedin/controller/QuestionController.php");
require_once("/loggedin/controller/CreateQuestion.php");
require_once("/loggedin/view/GameView.php");
require_once("/loggedin/controller/AlterQuestion.php");
require_once("/loggedin/controller/StartGame.php");

class Game {
	/**
	 * @var \loggedin\view\MemberAreaView
	 */
	private $memberAreaView;
	/**
	 * @var \login\view\HTMLView
	 */
	private $pageView;
	/**
	 * @var \loggedin\view\View
	 */
	private $view;
	/**
	 * @var \loggedin\view\view
	 */
	private $questionView;
	/**
	 * @var \loggedin\model\GetQuestions
	 */
	private $getQuestions;
	/**
	 * @var \login\view\UserInfo
	 */
	private $userInfo;
	/**
	 * @var \loggedin\controller\GetQuestions
	 */
	private $questionController;
	/**
	 * @var \loggedin\controller\CreateQuestion
	 */
	private $createQuestion;
	/**
	 * @var \loggedin\view\GameView
	 */
	private $gameView;
	/**
	 * @var \loggedin\controller\AlterQuestion
	 */
	private $alterQuestionController;
	/**
	 * @var \loggedin\controller\StartGame
	 */
	private $startGame;

	function __construct() {
		$this->memberAreaView = new \loggedin\view\MemberAreaView();
		$this->pageView = new \login\view\HTMLView();
		$this->view = new \common\view\View();
		$this->questionView = new \loggedin\view\QuestionView();
		$this->getQuestions = new \loggedin\model\GetQuestions();
		$this->userInfo = new \login\view\UserInfo();
		$this->questionController = new \loggedin\controller\QuestionController(
																			$this->getQuestions,
																			$this->userInfo,
																			$this->questionView);
		$this->createQuestion = new \loggedin\controller\CreateQuestion($this->questionView);
		$this->gameView = new \loggedin\view\GameView();
		$this->alterQuestionController = new \loggedin\controller\AlterQuestion($this->questionView);
		$this->startGame = new \loggedin\controller\StartGame($this->gameView);
	}

	/**
	 * @return string HTML
	 */
	public function getMemberPages() {
		if ($this->memberAreaView->getLogoutInfo()) {
			return $this->logoutUser();
		}
		if($this->gameView->getStartGameInfo()) {
			return $this->startGame->showQuestion();
		}
		if($this->gameView->getQuestionRepliedInfo()) {
			$this->startGame->correctAnswer();
			$this->view->refreshStartGame();
		}
		if ($this->questionView->getCreateQuestionInfo()) {
			$htmlQuestions = $this->questionController->getLoggedinUsersQuestions();
			return $this->questionView->getCreateQuestionPage($htmlQuestions);
		}
		if($this->questionView->getAddQuestionInfo()) {
			$this->createQuestion->addQuestion();
			$htmlQuestions = $this->questionController->getLoggedinUsersQuestions();
			$this->view->relocateToCreateQuestion();
			return $this->questionView->getCreateQuestionPage($htmlQuestions);
		}
		if($this->questionView->getMyQuestionsInfo()) {
			$htmlQuestions = $this->questionController->getLoggedinUsersQuestions();
			return $this->questionView->getMyQuestionPage($htmlQuestions);
		}
		if($this->questionView->getAlterQuestionInfo()) {
			return $this->alterQuestionController->alterQuestionPage();
		}
		if($this->questionView->getChangeQuestionInfo()) {
			$this->alterQuestionController->changeQuestion();
			$this->view->relocate();
		}
		$this->gameView->removeQuestionAnswerID();
		$this->questionView->removeAlterQuestionIDCookie();
		$this->questionView->removeErrorMessage();
		return $this->memberAreaView->getMemberStartPage();
	}

	/**
	 * @return HTML string
	 */
	public function logoutUser() {
		session_destroy();
		$this->pageView->setLogoutMessage();
		$this->userInfo->removeUserLoggedInCookie();
		$this->gameView->removeQuestionAnswerID();
		$this->questionView->removeAlterQuestionIDCookie();
		$this->questionView->removeErrorMessage();
		$this->gameView->removeMessage();
		return $this->pageView->getLoginPage();
	}
}